﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AMVCommander
{
    public partial class FormEditList : Form
    {
        bool bOk = false;
        List<string> strL;
        int StartHeight;
        int step = 24;

        public FormEditList()
        {
            InitializeComponent();
        }

        private void FormEditList_Shown(object sender, EventArgs e)
        {
            txbNew.Focus();
        }

        public bool ShowDialog(List<string> strL)
        {
            bOk = false;
            this.strL = strL;
            StartHeight = Height;
            foreach (string str in strL) AddLabel(str);

            ShowDialog();
            return bOk;
        }

        void AddLabel(string text)
        {
            Label lbl = new Label();
            lbl.Text = text;
            lbl.Font = new Font(lbl.Font, FontStyle.Bold);
            lbl.ForeColor = Color.Blue;
            lbl.AutoSize = false;
            lbl.Size = new Size(pnlTags.Width - 6, step);
            lbl.Font = new Font(lbl.Font.FontFamily, 12, FontStyle.Bold);
            lbl.Location = new Point(3, 2 + pnlTags.Controls.Count * step);
            lbl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            lbl.MouseDoubleClick += new MouseEventHandler(LblFlt_MouseDoubleClick);
            lbl.MouseEnter += new EventHandler(LblFlt_MouseEnter);
            lbl.MouseLeave += new EventHandler(LblFlt_MouseLeave);
            pnlTags.Controls.Add(lbl);
            RePosFltControls();
        }

        /// <summary> Удаление тега из фильтра по двойному клику </summary>
        void LblFlt_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            pnlTags.Controls.Remove((Label)sender);
            ((Label)sender).Dispose();
            RePosFltControls();
        }

        void LblFlt_MouseEnter(object sender, EventArgs e)
        {
            ((Label)sender).BackColor = SystemColors.ControlLight;
        }

        void LblFlt_MouseLeave(object sender, EventArgs e)
        {
            ((Label)sender).BackColor = SystemColors.Control;
        }

        /// <summary> Перепозиционирование контролов после добавления или удаления </summary>
        void RePosFltControls()
        {
            pnlTags.Height = 8 + pnlTags.Controls.Count * step;
            txbNew.Top = pnlTags.Bottom + 7;
            for (int i = 0; i < pnlTags.Controls.Count; i++) pnlTags.Controls[i].Location = new Point(3, 2 + i * step);
            Height = StartHeight + pnlTags.Controls.Count * step;
        }

        private void txbNew_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string newStr = txbNew.Text.Trim();

                // проверка на дубликаты
                bool bDupl = false;
                for (int i = 0; i < pnlTags.Controls.Count; i++) 
                    if (pnlTags.Controls[i].Text.ToUpper() == newStr.ToUpper())
                    {
                        MessageBox.Show("Такой элемент уже есть");
                        bDupl = true;
                    }

                if (bDupl)
                {
                    txbNew.Focus();
                    txbNew.Select(txbNew.Text.Length, 0);
                }
                else
                {
                    if (newStr != "")
                    {
                        AddLabel(newStr);
                        txbNew.Text = "";
                        txbNew.Focus();
                    }
                    if (!e.Control)
                    {
                        strL.Clear();
                        for (int i = 0; i < pnlTags.Controls.Count; i++) strL.Add(pnlTags.Controls[i].Text);
                        bOk = true;
                        Close();
                    }
                }
            }

            if (e.KeyCode == Keys.Escape) Close();
        }
 
    }
}
